package com.syp.auth.export.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class RequestExcluder {

	public static final boolean process(HttpServletRequest request, String[] excludeUriPatterns) {
		if (excludeUriPatterns == null || excludeUriPatterns.length == 0) {
			return false;
		}
		String url = RequestExcluder.takeOffContextPath(request);
		for (String patStr : excludeUriPatterns) {
			Pattern r = Pattern.compile(patStr);
			Matcher m = r.matcher(url);
			if (m.matches()) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 脱掉contextPath的访问地址
	 * 
	 * @param request
	 * @return
	 */
	public static final String takeOffContextPath(HttpServletRequest request) {
		String url = request.getRequestURI().substring(request.getContextPath().length());
		return url.isEmpty() ? "/" : url;
	}

}
